﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace DalGen
{
    public partial class TemplateExplorer : DockContent
    {
        public TemplateExplorer()
        {
            InitializeComponent();
        }

        private void TemplateExplorer_Load(object sender, EventArgs e)
        {
            BindIbatis();
            BindHelp();
        }


        public void BindIbatis()
        {
            string folderFullName = System.AppDomain.CurrentDomain.BaseDirectory + @"\Template\TemplateFile\IBatis\";
            DirectoryInfo TheFolder = new DirectoryInfo(folderFullName);
            //遍历文件
            treeView1.Nodes[0].Nodes[0].Nodes.Clear();
            foreach (FileInfo NextFile in TheFolder.GetFiles())
                treeView1.Nodes[0].Nodes[0].Nodes.Add(NextFile.Name);

        }

        public void BindHelp()
        {
            string folderFullName = System.AppDomain.CurrentDomain.BaseDirectory + @"\Template\TemplateFile\Help\";
            DirectoryInfo TheFolder = new DirectoryInfo(folderFullName);
            //遍历文件
            treeView1.Nodes[3].Nodes.Clear();
            foreach (FileInfo NextFile in TheFolder.GetFiles())
                treeView1.Nodes[3].Nodes.Add(NextFile.Name);

        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            string fileName = e.Node.Text;
            if (!fileName.EndsWith(".vm"))
                return;

            string nodeName = e.Node.Parent.Text;

            string filePath = System.AppDomain.CurrentDomain.BaseDirectory + @"\Template\TemplateFile\" + nodeName + "\\" + fileName;

            bool showForm = true;

            foreach (IDockContent content in DockPanel.Documents)
            {

                if (content.DockHandler.TabText == fileName)
                {
                    content.DockHandler.Activate();
                    showForm = false;
                    break;
                }
                

            }
            if (!showForm)
                return;

            DocForm form = new DocForm();
            form.FilePath = filePath;
            form.TabText = fileName;
            form.Show(this.DockPanel);
        }

    }
}
